package com.yupi.yuoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.yuoj.common.ErrorCode;
import com.yupi.yuoj.exception.ThrowUtils;
import com.yupi.yuoj.model.dto.contentrecommendation.ContentRecommendationQueryRequest;
import com.yupi.yuoj.model.entity.Content;
import com.yupi.yuoj.model.entity.ContentRecommendation;
import com.yupi.yuoj.model.vo.ContentRecommendationVO;
import com.yupi.yuoj.service.ContentRecommendationService;
import com.yupi.yuoj.mapper.ContentRecommendationMapper;
import com.yupi.yuoj.service.ContentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author admin
* @description 针对表【content_recommendation(内容推荐关联表)】的数据库操作Service实现
* @createDate 2025-11-01 21:28:47
*/
@Service
public class ContentRecommendationServiceImpl extends ServiceImpl<ContentRecommendationMapper, ContentRecommendation>
    implements ContentRecommendationService{

    @Resource
    private ContentService contentService;

    @Override
    public void validContentRecommendation(ContentRecommendation contentRecommendation, boolean add) {
        if (contentRecommendation == null) {
            throw new RuntimeException("内容推荐不能为空");
        }
        
        Long contentId = contentRecommendation.getContentId();
        Long relatedContentId = contentRecommendation.getRelatedContentId();
        String relationType = contentRecommendation.getRelationType();
        
        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(contentId == null || contentId <= 0, ErrorCode.PARAMS_ERROR, "内容ID不能为空");
            ThrowUtils.throwIf(relatedContentId == null || relatedContentId <= 0, ErrorCode.PARAMS_ERROR, "关联内容ID不能为空");
            ThrowUtils.throwIf(StringUtils.isBlank(relationType), ErrorCode.PARAMS_ERROR, "关联类型不能为空");
        }
        
        // 校验关联类型
        if (StringUtils.isNotBlank(relationType)) {
            if (!"similar".equals(relationType) && !"series".equals(relationType) && !"prerequisite".equals(relationType)) {
                throw new RuntimeException("关联类型不合法");
            }
        }
        
        // 校验内容是否存在
        if (contentId != null && contentId > 0) {
            Content content = contentService.getById(contentId);
            ThrowUtils.throwIf(content == null, ErrorCode.NOT_FOUND_ERROR, "内容不存在");
        }
        
        if (relatedContentId != null && relatedContentId > 0) {
            Content relatedContent = contentService.getById(relatedContentId);
            ThrowUtils.throwIf(relatedContent == null, ErrorCode.NOT_FOUND_ERROR, "关联内容不存在");
        }
    }

    @Override
    public QueryWrapper<ContentRecommendation> getQueryWrapper(ContentRecommendationQueryRequest contentRecommendationQueryRequest) {
        QueryWrapper<ContentRecommendation> queryWrapper = new QueryWrapper<>();
        if (contentRecommendationQueryRequest == null) {
            return queryWrapper;
        }
        
        Long id = contentRecommendationQueryRequest.getId();
        Long contentId = contentRecommendationQueryRequest.getContentId();
        Long relatedContentId = contentRecommendationQueryRequest.getRelatedContentId();
        String relationType = contentRecommendationQueryRequest.getRelationType();
        String sortField = contentRecommendationQueryRequest.getSortField();
        String sortOrder = contentRecommendationQueryRequest.getSortOrder();
        
        // 拼接查询条件
        queryWrapper.eq(id != null && id > 0, "id", id);
        queryWrapper.eq(contentId != null && contentId > 0, "content_id", contentId);
        queryWrapper.eq(relatedContentId != null && relatedContentId > 0, "related_content_id", relatedContentId);
        queryWrapper.eq(StringUtils.isNotBlank(relationType), "relation_type", relationType);
        
        // 排序规则
        queryWrapper.orderBy(StringUtils.isNotBlank(sortField),
                sortOrder.equals("ascend"), sortField);
        
        return queryWrapper;
    }

    @Override
    public ContentRecommendationVO getContentRecommendationVO(ContentRecommendation contentRecommendation, HttpServletRequest request) {
        ContentRecommendationVO contentRecommendationVO = ContentRecommendationVO.objToVo(contentRecommendation);
        
        // 关联查询内容信息
        Long contentId = contentRecommendation.getContentId();
        Long relatedContentId = contentRecommendation.getRelatedContentId();
        
        if (contentId != null && contentId > 0) {
            Content content = contentService.getById(contentId);
            if (content != null) {
                contentRecommendationVO.setContent(contentService.getContentVO(content, request));
            }
        }
        
        if (relatedContentId != null && relatedContentId > 0) {
            Content relatedContent = contentService.getById(relatedContentId);
            if (relatedContent != null) {
                contentRecommendationVO.setRelatedContent(contentService.getContentVO(relatedContent, request));
            }
        }
        
        return contentRecommendationVO;
    }

    @Override
    public Page<ContentRecommendationVO> getContentRecommendationVOPage(Page<ContentRecommendation> contentRecommendationPage, HttpServletRequest request) {
        List<ContentRecommendation> contentRecommendationList = contentRecommendationPage.getRecords();
        Page<ContentRecommendationVO> contentRecommendationVOPage = new Page<>(contentRecommendationPage.getCurrent(), contentRecommendationPage.getSize(), contentRecommendationPage.getTotal());
        if (contentRecommendationList.isEmpty()) {
            return contentRecommendationVOPage;
        }
        
        // 填充信息
        List<ContentRecommendationVO> contentRecommendationVOList = contentRecommendationList.stream()
                .map(contentRecommendation -> getContentRecommendationVO(contentRecommendation, request))
                .collect(Collectors.toList());
        
        contentRecommendationVOPage.setRecords(contentRecommendationVOList);
        return contentRecommendationVOPage;
    }
}




